<?php
namespace app\backstage\validate;
use think\Validate;

class BackhaulLink extends Validate
{
    protected $rule = [
        'channel_id'            => 'require|gt:0',
        'link_id'               => 'require|gt:0',
        'id'                    => 'require|gt:0',
        'advert_media_id'       => 'require|gt:0',
        'ratio'                 => 'require|gt:0',
        'report_caliber'        => 'require|gt:0',
        'uid'                   => 'require|gt:0',
        'linkIds'               => 'require|checkIds'
    ];

    protected $message  =   [
        'id.require'                    => '回传记录ID不能为空',
        'id.gt'                         => '回传记录ID不能为空',
        'channel_id.require'            => '请选择所属渠道',
        'channel_id.gt'                 => '请选择所属渠道',
        'link_id.require'               => '请选择推广链接',
        'link_id.gt'                    => '请选择推广链接',
        'advert_media_id.require'       => '请选择投放媒体',
        'advert_media_id.gt'            => '请选择投放媒体',
        'ratio.require'                 => '请设置回传比例',
        'ratio.gt'                      => '请设置回传比例',
        'report_caliber.require'        => '请选择上报口径',
        'report_caliber.gt'             => '请选择上报口径',
        'uid.require'                   => '用户ID不能为空',
        'uid.gt'                        => '用户ID不能小于0',
        'linkIds'                       => '请选择推广链接',
    ];

    protected $scene = [
        'add'       => ['channel_id','advert_media_id','report_caliber'],
        'add_channel'       => ['advert_media_id','report_caliber'],
        'edit'      => ['id','channel_id','advert_media_id','report_caliber'],
        'edit_channel'      => ['id','advert_media_id','report_caliber'],
        'info'      => ['id'],
        'user_active' => ['uid','advert_media_id'],
    ];

    // 自定义验证规则
    protected function checkIds($value, $rule, $data = [])
    {
        if (!preg_match('#^[1-9][0-9]*(,[1-9][0-9]*)*$#', $value)) {
            return '参数异常';
        }
        return true;
    }
}